package
{
    import app.SWFInfoTreeEntry;
    import app.configuration.ConfigManger;
    import app.configuration.NodeFileMeta;
    import app.debug.APPLog;
    import app.utils.InvokeArgumentManger;

    import copyengine.infoTree.meta.CESMetaFacade;

    import flash.desktop.NativeApplication;
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.events.Event;
    import flash.events.InvokeEvent;
    import flash.events.UncaughtErrorEvent;

    import smartRun.SmartRunScene;

    [SWF(width="640", height="480", backgroundColor="#ffffff", frameRate="24")]
    public class SWFInfoTree extends Sprite
    {
        public static var gameStage:Stage;
        private var mCurrentAnalyst:SWFInfoTreeEntry;
        private var mSmartRunScene:SmartRunScene;

        public function SWFInfoTree()
        {
            NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, handleOnInvoke);
            this.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onGlobalError);
        }

        private function handleOnInvoke(event:InvokeEvent):void
        {
            gameStage = this.stage;
            //处理所有的参数
            InvokeArgumentManger.instance.initialize(event.arguments);
            //初始化Log
            APPLog.initialize(this.stage);
            //Import所有Meta
            CESMetaFacade.initialize();
            //初始化Config信息
            ConfigManger.instance.initialize(event.arguments, onConfigMangerInitComplete);
        }

        private function onGlobalError(e:Event):void
        {
            APPLog.err(e.toString());
            e.preventDefault();
            e.stopImmediatePropagation();
            e.stopPropagation();
        }

        private function onConfigMangerInitComplete():void
        {
            var isSmartRun:Boolean = InvokeArgumentManger.instance.isContainArgument("isSmartRun") ? InvokeArgumentManger.instance.getInvokeArgument("isSmartRun") : false;
            if (isSmartRun) {
                mSmartRunScene = new SmartRunScene();
                mSmartRunScene.initialize();
                this.addChild(mSmartRunScene.getContainer());
            }
            else {
                //命令行模式 扫描所有的SWF
                analyzeSWFFile();
            }
        }

        /**
         * 分析每个SWF文件(每个SWF文件都是单独Load单独分析,分析完成后及卸载)
         */
        private function analyzeSWFFile():void
        {
            APPLog.log("SwfInfoTree is start , please do not close infoTree...");
            var allFileNodeMetaVector:Vector.<NodeFileMeta> = ConfigManger.instance.allNodeFileVector;
            for each (var nodeConfigFile:NodeFileMeta in allFileNodeMetaVector) {
                if (!nodeConfigFile.isAnyliseFinish) {
                    mCurrentAnalyst = new SWFInfoTreeEntry();
                    mCurrentAnalyst.analyzeSWFFile(nodeConfigFile, analyzeSWFFile);
                    return;
                }
            }
            APPLog.log(".........");
            APPLog.log(".........");
            APPLog.log(".........");
            APPLog.log(".........");
            APPLog.log("All Finish!!!!!!");
            !APPLog.isHaveError() && NativeApplication.nativeApplication.exit(0);
        }

    }
}
